function rpc_model = read(rpc_path)
%hsRPC.read 读取标准RPC文件
%
% Syntax:
%   rpc_model = hsRPC.read(rpc_path);
% 
% Params:
%   - rpc_path  [required] [[char], [string]]  RPC文件路径
%
% Return:
%   - rpc_model  RPC结构体
%
% Author: 
%   iam002, 2025年2月28日
%
% Update: 
%   iam002, 2025年2月28日
%
% Matlab Version: R2023a

    rpc_model = parseStandardFile(rpc_path);

end

function rpc_model = parseStandardFile(rpc_path)

    % 解析标准的RPC文件
    parseCase1 = @(fid, tag_name)(sscanf(fgetl(fid), sprintf('%s = "%%[^"]";', tag_name)));
    parseCase2 = @(fid, tag_name)(sscanf(fgetl(fid), sprintf('\t%s = %%f;', tag_name)));

    rpc_model = struct('m_className', 'RPC');
    rpc_model.m_filepath = char(rpc_path);
    rpc_model.m_direction = 'unkown';
    rpc_model.m_width = -1;
    rpc_model.m_height = -1;

    %%
    fid = fopen(rpc_path, "r");
    if fid == -1
        error("cannot open: %s\n", rpc_path);
    end

    %%
    tag_name = 'satId';
    rpc_model.(tag_name) = parseCase1(fid, tag_name);
    
    tag_name = 'bandId';
    rpc_model.(tag_name) = parseCase1(fid, tag_name);
    
    tag_name = 'SpecId';
    rpc_model.(tag_name) = parseCase1(fid, tag_name);
    
    %%
    fgetl(fid);
    
    tag_name = 'errBias';
    rpc_model.(tag_name) = parseCase2(fid, tag_name);
    
    tag_name = 'errRand';
    rpc_model.(tag_name) = parseCase2(fid, tag_name);
    
    tag_name = 'lineOffset';
    rpc_model.(tag_name) = parseCase2(fid, tag_name);
    
    tag_name = 'sampOffset';
    rpc_model.(tag_name) = parseCase2(fid, tag_name);
    
    tag_name = 'latOffset';
    rpc_model.(tag_name) = parseCase2(fid, tag_name);
    
    tag_name = 'longOffset';
    rpc_model.(tag_name) = parseCase2(fid, tag_name);
    
    tag_name = 'heightOffset';
    rpc_model.(tag_name) = parseCase2(fid, tag_name);
    
    tag_name = 'lineScale';
    rpc_model.(tag_name) = parseCase2(fid, tag_name);
    
    tag_name = 'sampScale';
    rpc_model.(tag_name) = parseCase2(fid, tag_name);
    
    tag_name = 'latScale';
    rpc_model.(tag_name) = parseCase2(fid, tag_name);
    
    tag_name = 'longScale';
    rpc_model.(tag_name) = parseCase2(fid, tag_name);
    
    tag_name = 'heightScale';
    rpc_model.(tag_name) = parseCase2(fid, tag_name);
    
    %% 
    fgetl(fid);
    tag_name = 'lineNumCoef';
    tmp = zeros(20, 1);
    for i = 1:20
            tmp(i) = sscanf(fgetl(fid), '%f');
    end
    rpc_model.(tag_name) = tmp;
    
    %% 
    fgetl(fid);
    tag_name = 'lineDenCoef';
    tmp = zeros(20, 1);
    for i = 1:20
            tmp(i) = sscanf(fgetl(fid), '%f');
    end
    rpc_model.(tag_name) = tmp;
    
    %% 
    fgetl(fid);
    tag_name = 'sampNumCoef';
    tmp = zeros(20, 1);
    for i = 1:20
            tmp(i) = sscanf(fgetl(fid), '%f');
    end
    rpc_model.(tag_name) = tmp;
    
    %% 
    fgetl(fid);
    tag_name = 'sampDenCoef';
    tmp = zeros(20, 1);
    for i = 1:20
            tmp(i) = sscanf(fgetl(fid), '%f');
    end
    rpc_model.(tag_name) = tmp;
    
    %%
    fclose(fid);
end
